#include "ReactorThread.h"
#include <assert.h>
#include "EventLoop.h"

namespace dsth {

ReactorThread::ReactorThread() :
#ifndef IS_WINDOWS
    mutex_(),
    event_(mutex_),
#endif
    loop_ptr_(NULL),
    started_(false) {
    thread_.SetSvcFunc(std::make_pair(this, &ReactorThread::Loop));
}

ReactorThread::~ReactorThread() {
    Join();
}

EventLoop* ReactorThread::Run() {
    assert(!started_);
    thread_.Run();
    started_ = true;
    return &loop_;
}

void ReactorThread::Join() {
    if (started_) {
        loop_.Quit();
        thread_.Join();
        started_ = false;
    }
}

void ReactorThread::Loop(const char* thread_name) {
    loop_.Loop();
}

} // namespace
